package com.sangeng.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sangeng.domain.ResponseResult;
import com.sangeng.entity.Category;
import com.sangeng.entity.Role;
import com.sangeng.enums.AppHttpCodeEnum;
import com.sangeng.service.ICategoryService;
import com.sangeng.utils.BeanCopyUtils;
import com.sangeng.utils.WebUtils;
import com.sangeng.vo.CategoryVo;
import com.sangeng.vo.ExcelCategoryVo;
import com.sangeng.vo.PageVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/content/category")
public class CategoryController {
    @Autowired
    private ICategoryService categoryService;

    @GetMapping("/listAllCategory")
    public ResponseResult listAllCategory() {
        List<CategoryVo> list = categoryService.listAllCategory();
        return ResponseResult.okResult(list);
    }


    @PreAuthorize("@ps.hasPermission('content:category:export')")
    @GetMapping("/export")
    public void export(HttpServletResponse response) {
        try {
            //设置下载文件的请求头
            WebUtils.setDownLoadHeader("分类.xlsx", response);
            //获取需要导出的数据
            List<Category> categoryVos = categoryService.list();

            List<ExcelCategoryVo> excelCategoryVos = BeanCopyUtils.copyBeanList(categoryVos, ExcelCategoryVo.class);
            //把数据写入到Excel中
            EasyExcel.write(response.getOutputStream(), ExcelCategoryVo.class).autoCloseStream(Boolean.FALSE).sheet("分类导出")
                    .doWrite(excelCategoryVos);

        } catch (Exception e) {
            //如果出现异常也要响应json
            ResponseResult result = ResponseResult.errorResult(AppHttpCodeEnum.SYSTEM_ERROR);
            WebUtils.renderString(response, JSON.toJSONString(result));
        }
    }

    @GetMapping("/list")
    public ResponseResult list(String name, String status, int pageNum, int pageSize) {
        LambdaQueryWrapper<Category> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.hasText(name), Category::getName, name);
        wrapper.eq(StringUtils.hasText(status), Category::getStatus, status);
        Page<Category> page = new Page<>(pageNum, pageSize);
        Page<Category> page1 = categoryService.page(page, wrapper);

        List<Category> categories = page1.getRecords();

        PageVo pageVo = new PageVo(categories, page1.getTotal());
        return ResponseResult.okResult(pageVo);
    }

    // 新增分类
    @PostMapping
    public ResponseResult add(@RequestBody Category category) {
        categoryService.save(category);
        return ResponseResult.okResult();
    }

    // 详情
    @GetMapping("/{id}")
    public ResponseResult getInfo(@PathVariable("id") Long id) {
        Category category = categoryService.getById(id);
        return ResponseResult.okResult(category);
    }

    // 更新分类
    @PutMapping
    public ResponseResult update(@RequestBody Category category) {
        categoryService.updateById(category);
        return ResponseResult.okResult();
    }

    // 删除分类
    @DeleteMapping("/{id}")
    public ResponseResult delete(@PathVariable("id") Long id) {
        categoryService.removeById(id);
        return ResponseResult.okResult();
    }

}
